/**************************************************************************
Copyright (C) jianbo miao Corporation.  All Rights Released.
this is a 3d engin named our_3d_engin.
our_3d_engin feature:
1:there is no float.
2:it do the 3d things all by softwear.
3:base on 1 and 2 , it can execution on arm which have no 3d hardwear accelerate.

  if you have any  suggestion or question,pls contact with me
  mail:miaojb@126.com
  msn:miaojianbo@hotmail.com
  qq:30209027
  
    2008/01/01
***************************************************************************/
#ifndef OUR_DRAW16_C
#define  OUR_DRAW16_C
//

#include "our_3d.h"
#include "our_draw16.h"
#include "our_draw32.h"

#if 0
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//==================================================================================
int OUR_Draw_Line16(int x0, int y0, 	// starting position 
                    int x1, int y1, 	// ending position
                    int color,     	// color index
                    UCHAR *vb_start, int lpitch_2) // video buffer and memory pitch
{
    // this function draws a line from xo,yo to x1,y1 using differential error
    // terms (based on Bresenahams work)
    
    int dx,             // difference in x's
        dy,             // difference in y's
        dx2,            // dx,dy * 2
        dy2, 
        x_inc,          // amount in pixel space to move during drawing
        y_inc,          // amount in pixel space to move during drawing
        error,          // the discriminant i.e. error i.e. decision variable
        index;          // used for looping
    USHORT *vb_start2;
    
    lpitch_2 = lpitch_2>>1 ; // USHORT strided lpitch
    
    // pre-compute first pixel address in video buffer based on 16bit data
    vb_start2= (USHORT*)vb_start + x0 + y0*lpitch_2;
    // pre-compute first pixel address in video buffer based on 16bit data
    //UINT*vb_start2 = (UINT*)vb_start + x0 + y0*lpitch_2;
    
    // compute horizontal and vertical deltas
    dx = x1-x0;
    dy = y1-y0;
    
    // test which direction the line is going in i.e. slope angle
    if (dx>=0)
    {
        x_inc = 1;
    } // end if line is moving right
    else
    {
        x_inc = -1;
        dx    = -dx;  // need absolute value
    } // end else moving left
    // test y component of slope
    if (dy>=0)
    {
        y_inc = lpitch_2;
    } // end if line is moving down
    else
    {
        y_inc = -lpitch_2;
        dy    = -dy;  // need absolute value
    } // end else moving up
    
    dx2 = dx << 1;// compute (dx,dy) * 2
    dy2 = dy << 1;
    
    if (dx > dy)// now based on which delta is greater we can draw the line
    {
        
        error = dy2 - dx; // initialize error term
        
        for (index=0; index <= dx; index++)// draw the line
        {
            
            *vb_start2 = (UINT)color;// set the pixel
            
            if (error >= 0) // test if error has overflowed
            {
                error-=dx2;
                
                vb_start2+=y_inc;// move to next line
            } // end if error overflowed
            
            error+=dy2;// adjust the error term
            
            vb_start2+=x_inc;// move to the next pixel
        } // end for
    } // end if |slope| <= 1
    else
    {
        // initialize error term
        error = dx2 - dy; 
        // draw the line
        for (index=0; index <= dy; index++)
        {
            // set the pixel
            *vb_start2 = (UINT)color;
            
            // test if error overflowed
            if (error >= 0)
            {
                error-=dy2;
                // move to next line
                vb_start2+=x_inc;
            } // end if error overflowed
            // adjust the error term
            error+=dx2;
            // move to the next pixel
            vb_start2+=y_inc;
        } // end for
    } // end else |slope| > 1
    // return success
    return(1);
} // end Draw_Line16
                    //=================================================================================
                    int OUR_Draw_Clip_Line16(int x0,int y0, int x1, int y1, int color, 
                        UCHAR *dest_buffer, int lpitch)
                    {
                        // this function draws a clipped line
                        
                        int cxs, cys,
                            cxe, cye;
                        
                        // clip and draw each line
                        cxs = x0;
                        cys = y0;
                        cxe = x1;
                        cye = y1;
                        
                        // clip the line
                        //if (OUR_Clip_Line(&cxs,&cys,&cxe,&cye))
                        OUR_Draw_Line16(cxs, cys, cxe,cye,color,dest_buffer,lpitch);
                        
                        // return success
                        return(1);
                        
                    } // end Draw_Clip_Line16
                    
                    //--------------------------------------------------------------------
                    
                    void OUR_Draw_Wire16( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch,int num)
                    {
                        UINT i1,i2,i3;
                        UINT poly;
                        //UINT num;
                        UINT poly_change;
                        FIX_POINT3D_PTR     point_work=obj->pPoints_work;
                        OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
                        num=obj->pTriangle_num_changed;
                        for (poly=0; poly <num; poly++)
                        {
                            //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
                            {
                                poly_change=obj->pTriangle_changed[poly];
                                i1=triangle_work[poly_change].p1;
                                i2=triangle_work[poly_change].p2;
                                i3=triangle_work[poly_change].p3;
                                OUR_Draw_Clip_Line16(point_work[i1].MM.x, 
                                    point_work[i1].MM.y,
                                    point_work[i2].MM.x, 
                                    point_work[i2].MM.y,
                                    // triangle_work[poly_change].CC_work.clor,
                                    obj->clor_work[poly_change],
                                    video_buffer, lpitch);
                                OUR_Draw_Clip_Line16(	point_work[i3].MM.x, 
                                    point_work[i3].MM.y,
                                    point_work[i2].MM.x, 
                                    point_work[i2].MM.y,
                                    // triangle_work[poly_change].CC_work.clor,
                                    obj->clor_work[poly_change],
                                    video_buffer, lpitch);
                                OUR_Draw_Clip_Line16(	point_work[i1].MM.x, 
                                    point_work[i1].MM.y,
                                    point_work[i3].MM.x, 
                                    point_work[i3].MM.y,
                                    // triangle_work[poly_change].CC_work.clor,
                                    obj->clor_work[poly_change],
                                    video_buffer, lpitch);
                                OUR_D_triangle_drawed_num++;
                                
                            }
                        }
                    } // end Draw_RENDERLIST4DV1_Wire
#endif
                    //===========================================================
                    
                    
                    
                    //--------------------------------------------------------------------------
#define DRAW_T_SHIFT 20
                    void OUR_Draw_Triangle_CLIP_2D16(	int x1,int y1,
                        int x2,int y2,
                        int x3,int y3,
                        CLR color,
                        UCHAR *dest_buffer, int mempitch)
                    {
                        int xs,xe,ks,ke,xinc;
                        int yinc;
                        CLR16 *dest;
                        color=OUR_RGB32_2_16(color);
                        //make sure y1<y2<y3
                        if(y1>y2)
                        {
                            OUR_SWAP(y1,y2,xs);
                            OUR_SWAP(x1,x2,xs);
                        }
                        if(y1>y3)
                        {
                            OUR_SWAP(y1,y3,xs);
                            OUR_SWAP(x1,x3,xs);
                        }
                        if(y2>y3)
                        {
                            OUR_SWAP(y2,y3,xs);
                            OUR_SWAP(x2,x3,xs);
                        }					//18t
                        
                        //mempitch>>=1;//(sizeof(dest)/2);
                        dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
                        if(y1<y2 )//the top part
                        {		
                            ks=((x1-x2)<<DRAW_T_SHIFT)/(y1-y2);		//slope 1 --> 2
                            ke=((x1-x3)<<DRAW_T_SHIFT)/(y1-y3);		//slope 1 --> 3
                            if(ks>ke)						//start slope must be smaller than the end one
                            {
                                OUR_SWAP(ks,ke,xs);
                            }		
                            xs=x1<<DRAW_T_SHIFT;
                            xe=xs;
                            //clip ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
                            yinc=y1;
                            if(y1<0)
                            {
                                if(y2>0)
                                {
                                    
                                    dest-=y1*mempitch;			//point to next line
                                    xs-=y1*ks;					//next line xs
                                    xe-=y1*ke;					//next line xe
                                    yinc=0;
                                }
                                else
                                {
                                    y1=y1-y2;
                                    
                                    
                                    dest-=y1*mempitch;			//point to next line
                                    xs-=y1*ks;					//next line xs
                                    xe-=y1*ke;					//next line xe
                                    yinc=0;
                                    goto P3_T;
                                }/**/
                            }
                            if(y2>=OUR_D_screem_h)
                            {	
                                for(;yinc<OUR_D_screem_h;yinc++)		//scan lines
                                {				
                                    //------------------------------------------------------	
                                    xinc=xs>>(DRAW_T_SHIFT);
                                    if(xinc<0)
                                    {					
                                        xinc=0;
                                    }
                                    y1=(xe>>(DRAW_T_SHIFT));
                                    if(y1>=OUR_D_screem_w)
                                    {
                                        y1=OUR_D_screem_w-1;
                                    }
                                    
                                    for(;xinc<=y1;)	
                                    {
                                        dest[xinc++] =color;
                                    }					
                                    dest+=mempitch;			//point to next line
                                    xs+=ks;					//next line xs
                                    xe+=ke;					//next line xe
                                }
                                return;
                            }
                            for(;yinc<y2;yinc++)		//scan lines
                            {
                                //------------------------------------------------------	
                                xinc=xs>>(DRAW_T_SHIFT);
                                if(xinc<0)
                                {					
                                    xinc=0;
                                }
                                y1=(xe>>(DRAW_T_SHIFT));
                                if(y1>=OUR_D_screem_w)
                                {
                                    y1=OUR_D_screem_w-1;
                                }
                                
                                for(;xinc<=y1;)	
                                {
                                    dest[xinc++] =color;
                                }					
                                dest+=mempitch;			//point to next line
                                xs+=ks;					//next line xs
                                xe+=ke;					//next line xe
                            }	
P3_T:		
                            if(y2<y3)//the butt part
                            {	
                                x1=((x2-x3)<<DRAW_T_SHIFT)/(y2-y3);
                                if(x1>ke)
                                {
                                    ks=x1;
                                }
                                else
                                {
                                    ke=x1;
                                }
                                if(y2<0)
                                {				
                                    dest-=y2*mempitch;			//point to next line
                                    xs-=y2*ks;					//next line xs
                                    xe-=y2*ke;					//next line xe
                                    
                                }/**/
                                if(y3>OUR_D_screem_h-1)
                                {
                                    y3=OUR_D_screem_h-1;
                                }
                                for(;yinc<=y3;yinc++)
                                {
                                    //------------------------------------------------------	
                                    xinc=xs>>(DRAW_T_SHIFT);
                                    if(xinc<0)
                                    {					
                                        xinc=0;
                                    }
                                    y1=(xe>>(DRAW_T_SHIFT));
                                    if(y1>=OUR_D_screem_w)
                                    {
                                        y1=OUR_D_screem_w-1;
                                    }
                                    
                                    for(;xinc<=y1;)	
                                    {
                                        dest[xinc++] =color;
                                    }					
                                    dest+=mempitch;			//point to next line
                                    xs+=ks;					//next line xs
                                    xe+=ke;					//next line xe
                                }		
                                
                            }
                            return;
}
else
{		
    if(y2<y3)//the butt part
    {	
        ks=((x2-x3)<<DRAW_T_SHIFT)/(y1-y3);
        ke=((x1-x3)<<DRAW_T_SHIFT)/(y1-y3);
        if(ks<ke)
        {
            OUR_SWAP(ks,ke,xs);
        }	
        if(x1<x2)
        {
            xs=x1<<DRAW_T_SHIFT;
            xe=(x2<<DRAW_T_SHIFT);//+(xei>>1);
        }
        else 
        {
            xs=x2<<DRAW_T_SHIFT;
            xe=(x1<<DRAW_T_SHIFT);//+(xei>>1);
            
        }
        //clip
        if(y1<0)
        {
            dest-=y1*mempitch;			//point to next line
            xs-=y1*ks;					//next line xs
            xe-=y1*ke;					//next line xe
            y1=0;
        }
        if(y3>=OUR_D_screem_h)	
        {
            y3=OUR_D_screem_h;
        }
        for(yinc=y1;yinc<y3;yinc++)
        {
            //------------------------------------------------------	
            xinc=xs>>(DRAW_T_SHIFT);
            if(xinc<0)
            {					
                xinc=0;
            }
            y1=(xe>>(DRAW_T_SHIFT));
            if(y1>=OUR_D_screem_w)
            {
                y1=OUR_D_screem_w-1;
            }
            
            for(;xinc<=y1;)	
            {
                dest[xinc++] =color;
            }					
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }		
    }
    return;
}

}
#if 0
void OUR_Draw_Triangle_2D16(	int x1,int y1,
                            int x2,int y2,
                            int x3,int y3,
                            CLR color,
                            UCHAR *dest_buffer, int mempitch)
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR16 *dest;
    color=OUR_RGB32_2_16(color);
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
    }					//18t
    
    //mempitch>>=1;//(sizeof(dest)/2);
    dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
    if(y1<y2 )//the top part
    {		
        ks=((x1-x2)<<DRAW_T_SHIFT)/(y1-y2);		//slope 1 --> 2
        ke=((x1-x3)<<DRAW_T_SHIFT)/(y1-y3);		//slope 1 --> 3
        if(ks>ke)						//start slope must be smaller than the end one
        {
            OUR_SWAP(ks,ke,xs);
        }		
        xs=x1<<DRAW_T_SHIFT;
        xe=xs;
        for(yinc=y1;yinc<y2;yinc++)		//scan lines
        {	
#if OUR_FAST_2D		
            y1=(xe>>DRAW_T_SHIFT)-8;xinc=xs>>DRAW_T_SHIFT;
            for(xinc=xs>>DRAW_T_SHIFT;xinc<=y1;)
            {
                dest[xinc++] =color;dest[xinc++] =color;
                dest[xinc++] =color;dest[xinc++] =color;
                dest[xinc++] =color;dest[xinc++] =color;
                dest[xinc++] =color;dest[xinc++] =color;
            }
            
            if(xinc<=y1+4)	
            {
                dest[xinc++] =color;dest[xinc++] =color;
                dest[xinc++] =color;dest[xinc++] =color;
            }	
            y1+=8;
            for(;xinc<=y1;xinc++)
            {
                dest[xinc] =color;
            }	
#else	
            y1=(xe>>DRAW_T_SHIFT);
            for(xinc=xs>>DRAW_T_SHIFT;xinc<=y1;)
            {
                dest[xinc++] =color;
            }
#endif
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }		
        if(y2<y3)//the butt part
        {	
            x1=((x2-x3)<<DRAW_T_SHIFT)/(y2-y3);
            if(x1>ke)
            {
                ks=x1;
            }
            else
            {
                ke=x1;
            }
            for(;yinc<=y3;yinc++)
            {			
                //y1=xe>>8;
#if OUR_FAST_2D		
                y1=(xe>>DRAW_T_SHIFT)-8;
                for(xinc=xs>>DRAW_T_SHIFT;xinc<=y1;)
                {
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                }
                
                if(xinc<=y1+4)	
                {
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                }	
                y1+=8;
                for(;xinc<=y1;xinc++)
                {
                    dest[xinc] =color;
                }	
#else	
                y1=(xe>>DRAW_T_SHIFT);
                for(xinc=xs>>DRAW_T_SHIFT;xinc<=y1;)
                {
                    dest[xinc++] =color;
                }
#endif
                dest+=mempitch;
                xs+=ks;
                xe+=ke;
            }		
            
        }
        return;
    }
    else
    {		
        if(y2<y3)//the butt part
        {	
            ks=((x2-x3)<<DRAW_T_SHIFT)/(y1-y3);
            ke=((x1-x3)<<DRAW_T_SHIFT)/(y1-y3);
            if(ks<ke)
            {
                OUR_SWAP(ks,ke,xs);
            }	
            if(x1<x2)
            {
                xs=x1<<DRAW_T_SHIFT;
                xe=(x2<<DRAW_T_SHIFT);//+(xei>>1);
            }
            else 
            {
                xs=x2<<DRAW_T_SHIFT;
                xe=(x1<<DRAW_T_SHIFT);//+(xei>>1);
                
            }
            for(yinc=y1;yinc<y3;yinc++)
            {			
                //y1=xe>>8;
#if OUR_FAST_2D		
                y1=(xe>>DRAW_T_SHIFT)-8;
                for(xinc=xs>>DRAW_T_SHIFT;xinc<=y1;)
                {
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                }
                
                if(xinc<=y1+4)	
                {
                    dest[xinc++] =color;dest[xinc++] =color;
                    dest[xinc++] =color;dest[xinc++] =color;
                }	
                y1+=8;
                for(;xinc<=y1;xinc++)
                {
                    dest[xinc] =color;
                }	
#else	
                y1=(xe>>DRAW_T_SHIFT);
                for(xinc=xs>>DRAW_T_SHIFT;xinc<=y1;)
                {
                    dest[xinc++] =color;
                }
#endif
                dest+=mempitch;
                xs+=ks;
                xe+=ke;
            }		
        }
        return;
    }
    
}
#endif
//--------------------------------------------------------------------------
//------------------------------------				
#define FILL_POINT	{				\
    dest[xinc] =color;	\
    xinc++;			\
}
//----------------------------------------------------------------------------

#define DRAW_P_SHFT_X 18
#define SHIFR_L 18
#define SHIFT 30
//------------------------------------------------------
#define FILE_GOURAUD	{\
    dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
    ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3)));\
    \
    k_r_inc+=kre;\
    k_g_inc+=kge;\
    k_b_inc+=kbe;\
}
//------------------------------------------------------


void OUR_Draw_Gouraud_CLIP_2D16(	int x1,int y1,
                                int x2,int y2,
                                int x3,int y3,
                                CLR color1,
                                CLR color2,
                                CLR color3,
                                UCHAR *dest_buffer, int mempitch)
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR rs,re,gs,ge,bs,be;
    int krs,kre,kgs,kge,kbs,kbe;
    int  k_r_inc;
    int  k_g_inc;
    int  k_b_inc;
    CLR16 *dest;
    /*
    x1=200;
    y1=0;
    x2=1;
    y2=200;
    x3=350;
    y3=350;
    
      
        color1 = 0x000000;
        color2 = 0xff0000;
        color3 = 0x000000;
    */
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
        OUR_SWAP(color1,color2,rs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
        OUR_SWAP(color1,color3,rs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
        OUR_SWAP(color2,color3,rs);
    }								//18t
    
    //mempitch>>=2;					//(sizeof(dest)/2);
    dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
    if(y1<y2 )						//the top part
    {
        xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
        xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
        ks=((x1-x2))*xs;				//slope 1 --> 2
        ke=((x1-x3))*xe;				//slope 1 --> 3
        xs>>=(DRAW_P_SHFT_X-SHIFR_L);
        xe>>=(DRAW_P_SHFT_X-SHIFR_L);
        if(ks>ke)						//start slope must be smaller than the end one
        {
            kre=(((color1&OUR_RGB_MASK0500)-(color2&OUR_RGB_MASK0500))>>16)*xs;
            krs=(((color1&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xe;
            
            kge=(((color1&OUR_RGB_MASK0060)-(color2&OUR_RGB_MASK0060))>>8)*xs;
            kgs=(((color1&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xe;
            
            kbe=(((color1&OUR_RGB_MASK0005)-(color2&OUR_RGB_MASK0005)))*xs;
            kbs=(((color1&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xe;
            OUR_SWAP(ks,ke,xs);
        }	
        else if(ks<ke)	
        {
            krs=(((color1&OUR_RGB_MASK0500)-(color2&OUR_RGB_MASK0500))>>16)*xs;
            kre=(((color1&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xe;
            
            kgs=(((color1&OUR_RGB_MASK0060)-(color2&OUR_RGB_MASK0060))>>8)*xs;
            kge=(((color1&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xe;
            
            kbs=(((color1&OUR_RGB_MASK0005)-(color2&OUR_RGB_MASK0005)))*xs;
            kbe=(((color1&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xe;
        }
        else
        {
            return;
        }
#define GND_KKKK 0
        if((ke-ks)<(1<<DRAW_P_SHFT_X))
        {      
            xs=((1<<(SHIFT-4)))/((ke-ks));
            kre=(((kre-krs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);		
            kge=(((kge-kgs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);		
            kbe=(((kbe-kbs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);
        }
        else
        {
            xs=((1<<(SHIFT)))/((ke-ks)>>GND_KKKK);
            kre=(((kre-krs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);		
            kge=(((kge-kgs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);		
            kbe=(((kbe-kbs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);
        }/**/
        xs=(x1<<(DRAW_P_SHFT_X));
        xe=(x1<<(DRAW_P_SHFT_X));
        
        rs=(color1&OUR_RGB_MASK0500)<<(SHIFR_L-16);		
        gs=(color1&OUR_RGB_MASK0060)<<(SHIFR_L-8);		
        bs=(color1&OUR_RGB_MASK0005)<<SHIFR_L;

		
        //clip ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
        yinc=y1;
        if(y1<0)
        {
            if(y2>0)
            {
                rs-=y1*krs;			
                gs-=y1*kgs;			
                bs-=y1*kbs;
                
                dest-=y1*mempitch;			//point to next line
                xs-=y1*ks;					//next line xs
                xe-=y1*ke;					//next line xe
                yinc=0;
            }
            else
            {
                y1=y1-y2;
                rs-=y1*krs;			
                gs-=y1*kgs;			
                bs-=y1*kbs;
                
                dest-=y1*mempitch;			//point to next line
                xs-=y1*ks;					//next line xs
                xe-=y1*ke;					//next line xe
                yinc=0;
                goto P3_G;
            }/**/
        }
        if(y2>=OUR_D_screem_h)
        {	
            for(;yinc<OUR_D_screem_h;yinc++)		//scan lines
            {	
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------	
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
                        ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3))&OUR_RGB_MASK005);
                }
                for(;xinc<=y1;)	
                {
                    FILE_GOURAUD;
                }		
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }
            return;
        }
        for(;yinc<y2;yinc++)		//scan lines
        {	
            k_r_inc=rs;
            k_g_inc=gs;
            k_b_inc=bs;
            //------------------------------------------------------	
            xinc=xs>>(DRAW_P_SHFT_X);
            if(xinc<0)
            {
                k_r_inc-=xinc*kre;
                k_g_inc-=xinc*kge;
                k_b_inc-=xinc*kbe;
                xinc=0;
            }
            y1=(xe>>(DRAW_P_SHFT_X));
            if(y1>=OUR_D_screem_w)
            {
                y1=OUR_D_screem_w-1;
            }
            else if(y1>0)
            {
                dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
                    ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3))&OUR_RGB_MASK005);
            }
            for(;xinc<=y1;)	
            {
                FILE_GOURAUD;
            }			
            rs+=krs;			
            gs+=kgs;			
            bs+=kbs;
            
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }
P3_G:
        if(y2<y3)						//the butt part
        {	
            y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
            x1=((x2-x3))*y1;
            y1>>=(DRAW_P_SHFT_X-SHIFR_L);
            if(x1>ke)
            {
                ks=x1;
                
                krs=(((color2&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*y1;			
                kgs=(((color2&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*y1;				
                kbs=(((color2&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*y1;
            }
            else
            {
                ke=x1;
            }
            if(y2<0)
            {
                rs-=y2*krs;			
                gs-=y2*kgs;			
                bs-=y2*kbs;
                
                dest-=y2*mempitch;			//point to next line
                xs-=y2*ks;					//next line xs
                xe-=y2*ke;					//next line xe
                
            }/**/
            if(y3>OUR_D_screem_h-1)
            {
                y3=OUR_D_screem_h-1;
            }
            for(;yinc<=y3;yinc++)
            {			
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
                        ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3))&OUR_RGB_MASK005);
                }
                for(;xinc<=y1;)	
                {
                    FILE_GOURAUD;
                }			
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }		
            
        }
        return;
}
#else

#endif
//---------------------------------------------------------------------
else if(y2<y3)//the butt part
{		
    
    xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
    ks=((x1-x3))*xs;				//slope 1 --> 2
    ke=((x2-x3))*xs;				//slope 1 --> 3
    if(ks>ke)						//start slope must be smaller than the end one
    {
        xe=((1<<SHIFR_L))/(x2-x1);	
        krs=(((color1&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xs;
        kre=(((color2&OUR_RGB_MASK0500)-(color1&OUR_RGB_MASK0500))>>16)*xe;
        
        kgs=(((color1&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xs;
        kge=(((color2&OUR_RGB_MASK0060)-(color1&OUR_RGB_MASK0060))>>8)*xe;
        
        kbs=(((color1&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xs;
        kbe=(((color2&OUR_RGB_MASK0005)-(color1&OUR_RGB_MASK0005)))*xe;
        
        rs=(color1&OUR_RGB_MASK0500)<<(SHIFR_L-16);			
        gs=(color1&OUR_RGB_MASK0060)<<(SHIFR_L-8);			
        bs=(color1&OUR_RGB_MASK0005)<<SHIFR_L;
        
        xs=x1<<SHIFR_L;
        xe=x2<<SHIFR_L;
    }	
    else if(ks<ke)	
    {
        xe=((1<<SHIFR_L))/(x1-x2);	
        krs=(((color2&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xs;
        kre=(((color1&OUR_RGB_MASK0500)-(color2&OUR_RGB_MASK0500))>>16)*xe;
        
        kgs=(((color2&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xs;
        kge=(((color1&OUR_RGB_MASK0060)-(color2&OUR_RGB_MASK0060))>>8)*xe;
        
        kbs=(((color2&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xs;
        kbe=(((color1&OUR_RGB_MASK0005)-(color2&OUR_RGB_MASK0005)))*xe;
        
        OUR_SWAP(ks,ke,xs);			
        
        rs=(color2&OUR_RGB_MASK0500)<<(SHIFR_L-16);			
        gs=(color2&OUR_RGB_MASK0060)<<(SHIFR_L-8);			
        bs=(color2&OUR_RGB_MASK0005)<<SHIFR_L;
        
        xe=x1<<SHIFR_L;
        xs=x2<<SHIFR_L;
    }
    else
    {
        return;
    }	
    //clip
    if(y1<0)
    {
        rs-=y1*krs;			
        gs-=y1*kgs;			
        bs-=y1*kbs;
        
        dest-=y1*mempitch;			//point to next line
        xs-=y1*ks;					//next line xs
        xe-=y1*ke;					//next line xe
        y1=0;
    }
    if(y3>=OUR_D_screem_h)	
    {
        y3=OUR_D_screem_h;
    }
    for(yinc=y1;yinc<y3;yinc++)		//scan lines
    {	
        
        k_r_inc=rs;
        k_g_inc=gs;
        k_b_inc=bs;
        //------------------------------------------------------
        xinc=xs>>(DRAW_P_SHFT_X);
        if(xinc<0)
        {
            k_r_inc-=xinc*kre;
            k_g_inc-=xinc*kge;
            k_b_inc-=xinc*kbe;
            xinc=0;
        }
        y1=(xe>>(DRAW_P_SHFT_X));
        if(y1>=OUR_D_screem_w)
        {
            y1=OUR_D_screem_w-1;
        }
        else if(y1>0)
        {
            dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
                ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3))&OUR_RGB_MASK005);
        }
        for(;xinc<=y1;)	
        {
            FILE_GOURAUD;
        }
        rs+=krs;			
        gs+=kgs;			
        bs+=kbs;
        
        dest+=mempitch;			//point to next line
        xs+=ks;					//next line xs
        xe+=ke;	
    }		
    return;
}

}

//------------------------------------------------------

#if 0
void OUR_Draw_Gouraud_2D16(	int x1,int y1,
                           int x2,int y2,
                           int x3,int y3,
                           CLR color1,
                           CLR color2,
                           CLR color3,
                           UCHAR *dest_buffer, int mempitch)
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR rs,re,gs,ge,bs,be;
    int krs,kre,kgs,kge,kbs,kbe;
    int  k_r_inc;
    int  k_g_inc;
    int  k_b_inc;
    CLR16 *dest;
    /*
    x1=200;
    y1=0;
    x2=1;
    y2=200;
    x3=350;
    y3=350;
    
      
        color1 = 0x000000;
        color2 = 0xff0000;
        color3 = 0x000000;
    */
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
        OUR_SWAP(color1,color2,rs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
        OUR_SWAP(color1,color3,rs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
        OUR_SWAP(color2,color3,rs);
    }								//18t
    
    //mempitch>>=2;					//(sizeof(dest)/2);
    dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
    if(y1<y2 )						//the top part
    {
        xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
        xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
        ks=((x1-x2))*xs;				//slope 1 --> 2
        ke=((x1-x3))*xe;				//slope 1 --> 3
        xs>>=(DRAW_P_SHFT_X-SHIFR_L);
        xe>>=(DRAW_P_SHFT_X-SHIFR_L);
        if(ks>ke)						//start slope must be smaller than the end one
        {
            kre=(((color1&OUR_RGB_MASK0500)-(color2&OUR_RGB_MASK0500))>>16)*xs;
            krs=(((color1&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xe;
            
            kge=(((color1&OUR_RGB_MASK0060)-(color2&OUR_RGB_MASK0060))>>8)*xs;
            kgs=(((color1&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xe;
            
            kbe=(((color1&OUR_RGB_MASK0005)-(color2&OUR_RGB_MASK0005)))*xs;
            kbs=(((color1&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xe;
            OUR_SWAP(ks,ke,xs);
        }	
        else if(ks<ke)	
        {
            krs=(((color1&OUR_RGB_MASK0500)-(color2&OUR_RGB_MASK0500))>>16)*xs;
            kre=(((color1&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xe;
            
            kgs=(((color1&OUR_RGB_MASK0060)-(color2&OUR_RGB_MASK0060))>>8)*xs;
            kge=(((color1&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xe;
            
            kbs=(((color1&OUR_RGB_MASK0005)-(color2&OUR_RGB_MASK0005)))*xs;
            kbe=(((color1&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xe;
        }
        else
        {
            return;
        }
#define GND_KKKK 0
        if((ke-ks)<(1<<DRAW_P_SHFT_X))
        {      
            xs=((1<<(SHIFT-4)))/((ke-ks));
            kre=(((kre-krs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);		
            kge=(((kge-kgs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);		
            kbe=(((kbe-kbs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);
        }
        else
        {
            xs=((1<<(SHIFT)))/((ke-ks)>>GND_KKKK);
            kre=(((kre-krs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);		
            kge=(((kge-kgs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);		
            kbe=(((kbe-kbs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);
        }/**/
        xs=(x1<<(DRAW_P_SHFT_X));
        xe=(x1<<(DRAW_P_SHFT_X));
        
        rs=(color1&OUR_RGB_MASK0500)<<(SHIFR_L-16);		
        gs=(color1&OUR_RGB_MASK0060)<<(SHIFR_L-8);		
        bs=(color1&OUR_RGB_MASK0005)<<SHIFR_L;
        for(yinc=y1;yinc<y2;yinc++)		//scan lines
        {	
            k_r_inc=rs;
            k_g_inc=gs;
            k_b_inc=bs;
            //------------------------------------------------------	
            xinc=xs>>(DRAW_P_SHFT_X);
            y1=(xe>>(DRAW_P_SHFT_X));
            //dest[xinc++] =((k_r_inc>>(SHIFR_L-16))&OUR_RGB_MASK0800)+\
            //	((k_g_inc>>(SHIFR_L-8))&OUR_RGB_MASK0080)+((k_b_inc>>SHIFR_L)&OUR_RGB_MASK0008);
            dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
                ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3))&OUR_RGB_MASK005);
#if OUR_FAST_2D
            for(;xinc<=y1-8;)	
            {
                FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
                FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
            }	
            
            if(xinc<=y1-4)	
            {
                FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
            }	
            for(;xinc<=y1;)	
            {
                FILE_GOURAUD;
            }	
#else
            for(;xinc<=y1;)	
            {
                FILE_GOURAUD;
            }	
#endif
            rs+=krs;			
            gs+=kgs;			
            bs+=kbs;
            
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }
        if(y2<y3)						//the butt part
        {	
            y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
            x1=((x2-x3))*y1;
            y1>>=(DRAW_P_SHFT_X-SHIFR_L);
            if(x1>ke)
            {
                ks=x1;
                
                krs=(((color2&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*y1;			
                kgs=(((color2&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*y1;				
                kbs=(((color2&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*y1;
            }
            else
            {
                ke=x1;
            }
            for(;yinc<=y3;yinc++)
            {			
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------
                xinc=xs>>(DRAW_P_SHFT_X);
                y1=(xe>>(DRAW_P_SHFT_X));
                //dest[xinc++] =((k_r_inc>>(SHIFR_L-16))&OUR_RGB_MASK0800)+\
                //	((k_g_inc>>(SHIFR_L-8))&OUR_RGB_MASK0080)+((k_b_inc>>SHIFR_L)&OUR_RGB_MASK0008);
                dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
                    ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3))&OUR_RGB_MASK005);
#if OUR_FAST_2D
                for(;xinc<=y1-8;)	
                {
                    FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
                    FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
                }	
                
                if(xinc<=y1-4)	
                {
                    FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
                }	
                for(;xinc<=y1;)	
                {
                    FILE_GOURAUD;
                }	
#else
                for(;xinc<=y1;)	
                {
                    FILE_GOURAUD;
                }	
#endif
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }		
            
        }
        return;
}
#else

#endif
//---------------------------------------------------------------------
else if(y2<y3)//the butt part
{		
    
    xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
    ks=((x1-x3))*xs;				//slope 1 --> 2
    ke=((x2-x3))*xs;				//slope 1 --> 3
    if(ks>ke)						//start slope must be smaller than the end one
    {
        xe=((1<<SHIFR_L))/(x2-x1);	
        krs=(((color1&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xs;
        kre=(((color2&OUR_RGB_MASK0500)-(color1&OUR_RGB_MASK0500))>>16)*xe;
        
        kgs=(((color1&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xs;
        kge=(((color2&OUR_RGB_MASK0060)-(color1&OUR_RGB_MASK0060))>>8)*xe;
        
        kbs=(((color1&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xs;
        kbe=(((color2&OUR_RGB_MASK0005)-(color1&OUR_RGB_MASK0005)))*xe;
        
        rs=(color1&OUR_RGB_MASK0500)<<(SHIFR_L-16);			
        gs=(color1&OUR_RGB_MASK0060)<<(SHIFR_L-8);			
        bs=(color1&OUR_RGB_MASK0005)<<SHIFR_L;
        
        xs=x1<<SHIFR_L;
        xe=x2<<SHIFR_L;
    }	
    else if(ks<ke)	
    {
        xe=((1<<SHIFR_L))/(x1-x2);	
        krs=(((color2&OUR_RGB_MASK0500)-(color3&OUR_RGB_MASK0500))>>16)*xs;
        kre=(((color1&OUR_RGB_MASK0500)-(color2&OUR_RGB_MASK0500))>>16)*xe;
        
        kgs=(((color2&OUR_RGB_MASK0060)-(color3&OUR_RGB_MASK0060))>>8)*xs;
        kge=(((color1&OUR_RGB_MASK0060)-(color2&OUR_RGB_MASK0060))>>8)*xe;
        
        kbs=(((color2&OUR_RGB_MASK0005)-(color3&OUR_RGB_MASK0005)))*xs;
        kbe=(((color1&OUR_RGB_MASK0005)-(color2&OUR_RGB_MASK0005)))*xe;
        
        OUR_SWAP(ks,ke,xs);			
        
        rs=(color2&OUR_RGB_MASK0500)<<(SHIFR_L-16);			
        gs=(color2&OUR_RGB_MASK0060)<<(SHIFR_L-8);			
        bs=(color2&OUR_RGB_MASK0005)<<SHIFR_L;
        
        xe=x1<<SHIFR_L;
        xs=x2<<SHIFR_L;
    }
    else
    {
        return;
    }								
    for(yinc=y1;yinc<y3;yinc++)		//scan lines
    {	
        
        k_r_inc=rs;
        k_g_inc=gs;
        k_b_inc=bs;
        //------------------------------------------------------
        
        xinc=xs>>SHIFR_L;
        y1=(xe>>SHIFR_L);
        //dest[xinc++] =((k_r_inc>>(SHIFR_L-16))&OUR_RGB_MASK0800)+\
        //	((k_g_inc>>(SHIFR_L-8))&OUR_RGB_MASK0080)+((k_b_inc>>SHIFR_L)&OUR_RGB_MASK0008);
        dest[xinc++] =((k_r_inc>>(SHIFR_L-8))&OUR_RGB_MASK500)+\
            ((k_g_inc>>(SHIFR_L-3))&OUR_RGB_MASK060)+((k_b_inc>>(SHIFR_L+3))&OUR_RGB_MASK005);
#if OUR_FAST_2D
        for(;xinc<=y1-8;)	
        {
            FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
            FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
        }	
        
        if(xinc<=y1-4)	
        {
            FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;FILE_GOURAUD;
        }	
        for(;xinc<=y1;)	
        {
            FILE_GOURAUD;
        }	
#else
        for(;xinc<=y1;)	
        {
            FILE_GOURAUD;
        }	
#endif
        rs+=krs;			
        gs+=kgs;			
        bs+=kbs;
        
        dest+=mempitch;			//point to next line
        xs+=ks;					//next line xs
        xe+=ke;	
    }		
    return;
}

}
#endif
//------------------------------------------------------


#define FILE_BMP16_1	{*dest_++ =\
    bmp_pt[((((k_r_inc&0xffc0000)>>(SHIFR_L-bmp->bits)))+(k_b_inc>>SHIFR_L))];\
}

#define FILE_BMP16	{\
    FILE_BMP16_1;\
    k_r_inc+=kre;\
    k_b_inc+=kbe;\
}
//------------------------------------------------------
void OUR_Draw_BMP_CLIP_PRE_2D16_Q(	int x1,int y1,int z1,
				                        int x2,int y2,int z2,
				                        int x3,int y3,int z3,
				                        CLR color1,
				                        CLR color2,
				                        CLR color3,
						    	UCHAR *dest_buffer, int mempitch,
						    	OUR_BMP_INFO* bmp
						    	)
{
	int xs,xe,ks,ke,xinc;
	int yinc;
	CLR rs,re,gs,ge,bs,be;
	int krs,kre,kgs,kge,kbs,kbe;
	int  k_r_inc;
	int  k_g_inc;
	int  k_b_inc;
	CLR16 *dest;
	CLR16 *dest_;
	CLR16 *bmp_pt;
/*
	x1=10<<1;
	y1=10<<1;
	x2=10<<1;
	y2=265<<1;
	x3=265<<1;
	y3=265<<1;



	color3 = 0x000080;
	color1 = 0x008080;
	color2 = 0x000000;
*/
	//make sure y1<y2<y3
	bmp_pt=((CLR16 *)bmp->bmp_data);
	if(y1>y2)
	{
		OUR_SWAP(y1,y2,xs);
		OUR_SWAP(x1,x2,xs);
		OUR_SWAP(z1,z2,xs);
		OUR_SWAP(color1,color2,rs);
	}
	if(y1>y3)
	{
		OUR_SWAP(y1,y3,xs);
		OUR_SWAP(x1,x3,xs);
		OUR_SWAP(z1,z3,xs);
		OUR_SWAP(color1,color3,rs);
	}
	if(y2>y3)
	{
		OUR_SWAP(y2,y3,xs);
		OUR_SWAP(x2,x3,xs);
		OUR_SWAP(z2,z3,xs);
		OUR_SWAP(color2,color3,rs);
	}								//18t

	//mempitch>>=2;					//(sizeof(dest)/2);
	dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
	if(y1<y2 )						//the top part
	{
		xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
		xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
		//xs=-reciprocal_look[-(y1-y2)];
		//xe=-reciprocal_look[-(y1-y3)];	
		ks=((x1-x2))*xs;				//slope 1 --> 2
		ke=((x1-x3))*xe;				//slope 1 --> 3
		xs>>=(DRAW_P_SHFT_X-SHIFR_L);
		xe>>=(DRAW_P_SHFT_X-SHIFR_L);
		if(ks>ke)						//start slope must be smaller than the end one
		{
			kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
			krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
			
			//kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
			//kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
			
			kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
			kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
			OUR_SWAP(ks,ke,xs);
		}	
		else if(ks<ke)	
		{
			krs=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
			kre=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
			
			//kgs=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
			//kge=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
			
			kbs=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
			kbe=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
		}
		else
		{
			return;
		}
#define BMP_KKKK 5
//this place must be carefull
		if((ke-ks)<(1<<DRAW_P_SHFT_X))
		{
			xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L-2)))/((ke-ks));// 28  -- 10 -20 =18 -10
			//xs=reciprocal_look[((ke-ks)>>8)]<<2;// 28  -- 10 -20 =18 -10
			//---------------------------------------------
			kre= (kre-krs);
			if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X) )
				kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
			else
				kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
			//----------------------------------------------
			//kge= (kge-kgs);
			//if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
			//	kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);	
			//else
			//	kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
			//----------------------------------------------
			kbe= (kbe-kbs);
			if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
				kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
			else
				kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);	
			
		}
		else
		{
			xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L)))/((ke-ks)>>3);
			//xs=reciprocal_look[((ke-ks)>>18)]>>6;
			//---------------------------------------------
			kre= (kre-krs);
			if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X))
				kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
			else
				kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
			//----------------------------------------------
			//kge= (kge-kgs);
			//if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
			//	kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);	
			//else
			//	kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
			//----------------------------------------------
			kbe= (kbe-kbs);
			if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
				kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
			else
				kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);	
		}
		xs=(x1<<(DRAW_P_SHFT_X));
		xe=(x1<<(DRAW_P_SHFT_X));

		rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);		
		//gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);		
		bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
		//clip ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
		yinc=y1;
		if(y1<0)
		{
			if(y2>0)
			{
				rs-=y1*krs;			
				//gs-=y1*kgs;			
				bs-=y1*kbs;
				
				dest-=y1*mempitch;			//point to next line
				xs-=y1*ks;					//next line xs
				xe-=y1*ke;					//next line xe
				yinc=0;
			}
			else
			{
				y1=y1-y2;
				rs-=y1*krs;			
				//gs-=y1*kgs;			
				bs-=y1*kbs;
				
				dest-=y1*mempitch;			//point to next line
				xs-=y1*ks;					//next line xs
				xe-=y1*ke;					//next line xe
				yinc=0;
				goto P3;
			}/**/
		}
		if(y2>=OUR_D_screem_h)
		{
			for(;yinc<OUR_D_screem_h;yinc++)		//scan lines
			{	
				//k_r_inc=rs;
				//k_g_inc=gs;
				//k_b_inc=bs;
                 k_r_inc=rs+(((kre*(xs<<12))>>20)>>8);
                //k_g_inc=gs;
                 k_b_inc=bs+(((kbe*(xs<<12))>>20)>>8);
				//------------------------------------------------------	
				xinc=xs>>(DRAW_P_SHFT_X);
				if(xinc<0)
				{
					k_r_inc-=xinc*kre;
					//k_g_inc-=xinc*kge;
					k_b_inc-=xinc*kbe;
					xinc=0;
				}
				y1=(xe>>(DRAW_P_SHFT_X));
				dest_=dest+xinc;
				if(y1>=OUR_D_screem_w)
				{
					y1=OUR_D_screem_w-1;
				}
				else if(y1>0)
				{
					//dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                                //FILE_BMP16_1;
				}
				(CLR16*)y1=dest+y1;
				for(;dest_<=(CLR16*)y1;)	
				{
					FILE_BMP16;
				}		
				rs+=krs;			
				//gs+=kgs;			
				bs+=kbs;
				
				dest+=mempitch;			//point to next line
				xs+=ks;					//next line xs
				xe+=ke;					//next line xe
			}
			return;
		}
		//ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
		//else if(y2>0)			
		{
			for(;yinc<y2;yinc++)		//scan lines
			{	
					//k_r_inc=rs;
				//k_g_inc=gs;
				//k_b_inc=bs;
                 k_r_inc=rs+(((kre*(xs<<12))>>20)>>8);
                //k_g_inc=gs;
                 k_b_inc=bs+(((kbe*(xs<<12))>>20)>>8);
				//------------------------------------------------------	
				xinc=xs>>(DRAW_P_SHFT_X);
				if(xinc<0)
				{
					k_r_inc-=xinc*kre;
					//k_g_inc-=xinc*kge;
					k_b_inc-=xinc*kbe;
					xinc=0;
				}
				y1=(xe>>(DRAW_P_SHFT_X));
				dest_=dest+xinc;
				if(y1>=OUR_D_screem_w)
				{
					y1=OUR_D_screem_w-1;
				}
				else if(y1>0)
				{
					//dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                                //FILE_BMP16_1;
                          }
				(CLR16*)y1=dest+y1;
				for(;dest_<=(CLR16*)y1;)	
				{
					FILE_BMP16;
				}		
				rs+=krs;			
				//gs+=kgs;			
				bs+=kbs;
				
				dest+=mempitch;			//point to next line
				xs+=ks;					//next line xs
				xe+=ke;					//next line xe
			}
		}
P3:	
		if(y2<y3)						//the butt part
		{	
			y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
			//y1=-reciprocal_look[-(y2-y3)];
			x1=((x2-x3))*y1;
			y1>>=(DRAW_P_SHFT_X-SHIFR_L);
			if(x1>ke)
			{
				ks=x1;

				krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*y1;			
				//kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*y1;				
				kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*y1;
			}
			else
			{
				ke=x1;
			}
			//ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
			if(y2<0)
			{
				rs-=y2*krs;			
				//gs-=y2*kgs;			
				bs-=y2*kbs;
				
				dest-=y2*mempitch;			//point to next line
				xs-=y2*ks;					//next line xs
				xe-=y2*ke;					//next line xe
				
			}/**/
			if(y3>OUR_D_screem_h-1)
			{
				y3=OUR_D_screem_h-1;
			}
			//ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
			for(;yinc<=y3;yinc++)
			{			
				//k_r_inc=rs;
				//k_g_inc=gs;
				//k_b_inc=bs;
                 k_r_inc=rs+(((kre*(xs<<12))>>20)>>8);
                //k_g_inc=gs;
                 k_b_inc=bs+(((kbe*(xs<<12))>>20)>>8);
				//------------------------------------------------------
				xinc=xs>>(DRAW_P_SHFT_X);
				if(xinc<0)
				{
					k_r_inc-=xinc*kre;
					//k_g_inc-=xinc*kge;
					k_b_inc-=xinc*kbe;
					xinc=0;
				}
				y1=(xe>>(DRAW_P_SHFT_X));
				dest_=dest+xinc;
				if(y1>=OUR_D_screem_w)
				{
					y1=OUR_D_screem_w-1;
				}
				else if(y1>0)
				{
					//dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                                //FILE_BMP16_1;
                          }
				(CLR16*)y1=dest+y1;
				for(;dest_<=(CLR16*)y1;)	
				{
					FILE_BMP16;
				}	
				rs+=krs;			
				//gs+=kgs;			
				bs+=kbs;
				
				dest+=mempitch;			//point to next line
				xs+=ks;					//next line xs
				xe+=ke;					//next line xe
			}		
			
		}
		return;
	}
#else

#endif
	//---------------------------------------------------------------------
	else if(y2<y3)//the butt part
	{		
		
		xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
		//xs=-reciprocal_look[-(y1-y3)];	//y1=y2
		ks=((x1-x3))*xs;				//slope 1 --> 2
		ke=((x2-x3))*xs;				//slope 1 --> 3
		if(ks>ke)						//start slope must be smaller than the end one
		{
			xe=((1<<SHIFR_L))/(x2-x1);	
			krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
			kre=(((color2&OUR_RGB_MASK8800)-(color1&OUR_RGB_MASK8800))>>16)*xe;
			
			//kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
			//kge=(((color2&OUR_RGB_MASK0080)-(color1&OUR_RGB_MASK0080))>>8)*xe;
			
			kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
			kbe=(((color2&OUR_RGB_MASK0088)-(color1&OUR_RGB_MASK0088)))*xe;
			
			rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
			//gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
			bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;

			xs=x1<<SHIFR_L;
			xe=x2<<SHIFR_L;
		}	
		else if(ks<ke)	
		{
			xe=((1<<SHIFR_L))/(x1-x2);	
			krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
			kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xe;
			
			//kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
			//kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xe;
			
			kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
			kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xe;
			
			OUR_SWAP(ks,ke,xs);			
			
			rs=(color2&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
			//gs=(color2&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
			bs=(color2&OUR_RGB_MASK0088)<<SHIFR_L;

			xe=x1<<SHIFR_L;
			xs=x2<<SHIFR_L;
		}
		else
		{
			return;
		}
		//clip
		if(y1<0)
		{
			rs-=y1*krs;			
			//gs-=y1*kgs;			
			bs-=y1*kbs;
			
			dest-=y1*mempitch;			//point to next line
			xs-=y1*ks;					//next line xs
			xe-=y1*ke;					//next line xe
			y1=0;
		}
		if(y3>=OUR_D_screem_h)	
		{
			y3=OUR_D_screem_h;
		}
		for(yinc=y1;yinc<y3;yinc++)		//scan lines
		{	
					
				//k_r_inc=rs;
				//k_g_inc=gs;
				//k_b_inc=bs;

                 k_r_inc=rs+(((kre*(xs<<12))>>20)>>8);
                //k_g_inc=gs;
                 k_b_inc=bs+(((kbe*(xs<<12))>>20)>>8);
//------------------------------------------------------
			xinc=xs>>SHIFR_L;
			y1=(xe>>SHIFR_L);
			if(xinc<0)
			{
				k_r_inc-=xinc*kre;
				//k_g_inc-=xinc*kge;
				k_b_inc-=xinc*kbe;
				xinc=0;
			}
			dest_=dest+xinc;
			if(y1>=OUR_D_screem_w)
			{
				y1=OUR_D_screem_w-1;
			}
			else if(y1>0)
			{
				//dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
               // FILE_BMP16_1;
             }
			(CLR16*)y1=dest+y1;
				for(;dest_<=(CLR16*)y1;)		
			{
				FILE_BMP16;
			}
			rs+=krs;			
			//gs+=kgs;			
			bs+=kbs;
			
			dest+=mempitch;			//point to next line
			xs+=ks;					//next line xs
			xe+=ke;	
		}		
		return;
	}
		
}



//				k_g_inc+=kge;\
//------------------------------------------------------*bmp->width&OUR_RGB_MASK0088
#define FILE_BMP	{\
    dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L)))<<(bmp->bits))+(k_b_inc>>SHIFR_L))];\
    \
    k_r_inc+=kre;\
    k_b_inc+=kbe;\
}

//------------------------------------------------------
void OUR_Draw_BMP_CLIP_PRE_2D16(	int x1,int y1,int z1,
                                int x2,int y2,int z2,
                                int x3,int y3,int z3,
                                CLR color1,
                                CLR color2,
                                CLR color3,
                                
                                UCHAR *dest_buffer, int mempitch,
                                OUR_BMP_INFO* bmp
                                )
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR rs,re,bs,be;
    int krs,kre,kbs,kbe;
    int  k_r_inc;
    //int  k_g_inc;gs,ge,kgs,kge,
    int  k_b_inc;
    CLR16 *dest;
    /*
    x1=10<<1;
    y1=10<<1;
    x2=10<<1;
    y2=265<<1;
    x3=265<<1;
    y3=265<<1;
    
      
        
          color3 = 0x000080;
          color1 = 0x008080;
          color2 = 0x000000;
    */
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
        OUR_SWAP(z1,z2,xs);
        OUR_SWAP(color1,color2,rs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
        OUR_SWAP(z1,z3,xs);
        OUR_SWAP(color1,color3,rs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
        OUR_SWAP(z2,z3,xs);
        OUR_SWAP(color2,color3,rs);
    }								//18t
    
    //mempitch>>=2;					//(sizeof(dest)/2);
    dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
    if(y1<y2 )						//the top part
    {
        xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
        xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
        ks=((x1-x2))*xs;				//slope 1 --> 2
        ke=((x1-x3))*xe;				//slope 1 --> 3
        xs>>=(DRAW_P_SHFT_X-SHIFR_L);
        xe>>=(DRAW_P_SHFT_X-SHIFR_L);
        if(ks>ke)						//start slope must be smaller than the end one
        {
            kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            //for z
            //kge=((z1-z2))*xs;
            //kgs=((z1-z3))*xe;
            
            kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
            OUR_SWAP(ks,ke,xs);
        }	
        else if(ks<ke)	
        {
            krs=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            kre=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            //for z
            //kgs=((z1-z2))*xs;
            //kge=((z1-z3))*xe;
            
            kbs=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbe=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
        }
        else
        {
            return;
        }
#define BMP_KKKK 5
        //this place must be carefull
        if((ke-ks)<(1<<DRAW_P_SHFT_X))
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L-2)))/((ke-ks));// 28  -- 10 -20 =18 -10
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X) )
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            //kge= (kge-kgs);
            //if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
            //	kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);	
            //else
            //	kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);	
            
        }
        else
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L)))/((ke-ks)>>3);
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X))
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            //kge= (kge-kgs);
            //if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
            //	kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);	
            //else
            //	kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);	
        }
        xs=(x1<<(DRAW_P_SHFT_X));
        xe=(x1<<(DRAW_P_SHFT_X));
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);		
        //gs=z1;		
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        //clip ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
        yinc=y1;
        if(y1<0)
        {
            if(y2>0)
            {
                rs-=y1*krs;			
                //gs-=y1*kgs;			
                bs-=y1*kbs;
                
                dest-=y1*mempitch;			//point to next line
                xs-=y1*ks;					//next line xs
                xe-=y1*ke;					//next line xe
                yinc=0;
            }
            else
            {
                y1=y1-y2;
                rs-=y1*krs;			
                //gs-=y1*kgs;			
                bs-=y1*kbs;
                
                dest-=y1*mempitch;			//point to next line
                xs-=y1*ks;					//next line xs
                xe-=y1*ke;					//next line xe
                yinc=0;
                goto P3;
            }/**/
        }
        if(y2>=OUR_D_screem_h)
        {
            for(;yinc<OUR_D_screem_h;yinc++)		//scan lines
            {
                k_r_inc=rs-(kre*((xs>>14)&0xf)>>4);
                //k_g_inc=gs;
                k_b_inc=bs-(kbe*((xs>>14)&0xf)>>4);
                //------------------------------------------------------	
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    //k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    //dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                }
                for(;xinc<=y1;)	
                {
                    FILE_BMP;
                }		
                rs+=krs;			
                //gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }
            return;
        }
        //ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
        //else if(y2>0)			
        {
            for(;yinc<y2;yinc++)		//scan lines
            {
                k_r_inc=rs-(kre*((xs>>14)&0xf)>>4);
                //k_g_inc=gs;
                k_b_inc=bs-(kbe*((xs>>14)&0xf)>>4);
                //------------------------------------------------------	
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    //k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    //dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                }
                for(;xinc<=y1;)	
                {
                    FILE_BMP;
                }		
                rs+=krs;			
                //gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }
        }
P3:	
        if(y2<y3)						//the butt part
        {	
            y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
            x1=((x2-x3))*y1;
            y1>>=(DRAW_P_SHFT_X-SHIFR_L);
            if(x1>ke)
            {
                ks=x1;
                
                krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*y1;			
                //kgs=(z2-z3)*y1;				
                kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*y1;
            }
            else
            {
                ke=x1;
            }
            //ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
            if(y2<0)
            {
                rs-=y2*krs;			
                //gs-=y2*kgs;			
                bs-=y2*kbs;
                
                dest-=y2*mempitch;			//point to next line
                xs-=y2*ks;					//next line xs
                xe-=y2*ke;					//next line xe
                
            }/**/
            if(y3>OUR_D_screem_h-1)
            {
                y3=OUR_D_screem_h-1;
            }
            //ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
            for(;yinc<=y3;yinc++)
            {
                k_r_inc=rs-(kre*((xs>>14)&0xf)>>4);
                //k_g_inc=gs;
                k_b_inc=bs-(kbe*((xs>>14)&0xf)>>4);
                //------------------------------------------------------
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    //k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    //dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                }
                for(;xinc<=y1;)	
                {
                    FILE_BMP;
                }	
                rs+=krs;			
                //gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }		
            
        }
        return;
}
#else

#endif
//---------------------------------------------------------------------
else if(y2<y3)//the butt part
{		
    
    xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
    ks=((x1-x3))*xs;				//slope 1 --> 2
    ke=((x2-x3))*xs;				//slope 1 --> 3
    if(ks>ke)						//start slope must be smaller than the end one
    {
        xe=((1<<SHIFR_L))/(x2-x1);	
        krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color2&OUR_RGB_MASK8800)-(color1&OUR_RGB_MASK8800))>>16)*xe;
        
        //kgs=(z1-z3)*xs;
        //kge=(z2-z1)*xe;
        
        kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color2&OUR_RGB_MASK0088)-(color1&OUR_RGB_MASK0088)))*xe;
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        //gs=z1;			
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xs=x1<<SHIFR_L;
        xe=x2<<SHIFR_L;
    }	
    else if(ks<ke)	
    {
        xe=((1<<SHIFR_L))/(x1-x2);	
        krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xe;
        
        //kgs=(z2-z3)*xs;
        //kge=(z1-z2)*xe;
        
        kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xe;
        
        OUR_SWAP(ks,ke,xs);			
        
        rs=(color2&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        //gs=z2;			
        bs=(color2&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xe=x1<<SHIFR_L;
        xs=x2<<SHIFR_L;
    }
    else
    {
        return;
    }
    //clip
    if(y1<0)
    {
        rs-=y1*krs;			
        //gs-=y1*kgs;			
        bs-=y1*kbs;
        
        dest-=y1*mempitch;			//point to next line
        xs-=y1*ks;					//next line xs
        xe-=y1*ke;					//next line xe
        y1=0;
    }
    if(y3>=OUR_D_screem_h)	
    {
        y3=OUR_D_screem_h;
    }
    for(yinc=y1;yinc<y3;yinc++)		//scan lines
    {	
                k_r_inc=rs-(kre*((xs>>14)&0xf)>>4);
                //k_g_inc=gs;
                k_b_inc=bs-(kbe*((xs>>14)&0xf)>>4);
        //------------------------------------------------------
        xinc=xs>>SHIFR_L;
        y1=(xe>>SHIFR_L);
        if(xinc<0)
        {
            k_r_inc-=xinc*kre;
            //k_g_inc-=xinc*kge;
            k_b_inc-=xinc*kbe;
            xinc=0;
        }
        if(y1>=OUR_D_screem_w)
        {
            y1=OUR_D_screem_w-1;
        }
        else if(y1>0)
        {
            //dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
        }
        for(;xinc<=y1;)	
        {
            FILE_BMP;
        }
        rs+=krs;			
        //gs+=kgs;			
        bs+=kbs;
        
        dest+=mempitch;			//point to next line
        xs+=ks;					//next line xs
        xe+=ke;	
    }		
    return;
}

}


#if 0

//------------------------------------------------------
void OUR_Draw_BMP_2D16(	int x1,int y1,
                       int x2,int y2,
                       int x3,int y3,
                       CLR color1,
                       CLR color2,
                       CLR color3,
                       UCHAR *dest_buffer, int mempitch,
                       OUR_BMP_INFO* bmp
                       )
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR rs,re,gs,ge,bs,be;
    int krs,kre,kgs,kge,kbs,kbe;
    int  k_r_inc;
    int  k_g_inc;
    int  k_b_inc;
    CLR16 *dest;
    /*
    x1=10<<1;
    y1=10<<1;
    x2=10<<1;
    y2=265<<1;
    x3=265<<1;
    y3=265<<1;
    
      
        
          color3 = 0x000080;
          color1 = 0x008080;
          color2 = 0x000000;
    */
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
        OUR_SWAP(color1,color2,rs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
        OUR_SWAP(color1,color3,rs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
        OUR_SWAP(color2,color3,rs);
    }								//18t
    
    //mempitch>>=2;					//(sizeof(dest)/2);
    dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
    if(y1<y2 )						//the top part
    {
        xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
        xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
        ks=((x1-x2))*xs;				//slope 1 --> 2
        ke=((x1-x3))*xe;				//slope 1 --> 3
        xs>>=(DRAW_P_SHFT_X-SHIFR_L);
        xe>>=(DRAW_P_SHFT_X-SHIFR_L);
        if(ks>ke)						//start slope must be smaller than the end one
        {
            kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            
            kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
            OUR_SWAP(ks,ke,xs);
        }	
        else if(ks<ke)	
        {
            krs=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            kre=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            
            kgs=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kge=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbs=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbe=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
        }
        else
        {
            return;
        }
#define BMP_KKKK 5
        //this place must be carefull
        if((ke-ks)<(1<<DRAW_P_SHFT_X))
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L-2)))/((ke-ks));// 28  -- 10 -20 =18 -10
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X) )
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            kge= (kge-kgs);
            if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
                kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);	
            else
                kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);	
            
        }
        else
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L)))/((ke-ks)>>3);
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X))
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            kge= (kge-kgs);
            if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
                kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);	
            else
                kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);	
        }
        xs=(x1<<(DRAW_P_SHFT_X));
        xe=(x1<<(DRAW_P_SHFT_X));
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);		
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);		
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        for(yinc=y1;yinc<y2;yinc++)		//scan lines
        {	
            k_r_inc=rs;
            k_g_inc=gs;
            k_b_inc=bs;
            //------------------------------------------------------	
            xinc=xs>>(DRAW_P_SHFT_X);
            y1=(xe>>(DRAW_P_SHFT_X));
            dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
            for(;xinc<=y1;)	
            {
                FILE_BMP;
            }		
            rs+=krs;			
            gs+=kgs;			
            bs+=kbs;
            
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }
        if(y2<y3)						//the butt part
        {	
            y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
            x1=((x2-x3))*y1;
            y1>>=(DRAW_P_SHFT_X-SHIFR_L);
            if(x1>ke)
            {
                ks=x1;
                
                krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*y1;			
                kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*y1;				
                kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*y1;
            }
            else
            {
                ke=x1;
            }
            for(;yinc<=y3;yinc++)
            {			
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------
                xinc=xs>>(DRAW_P_SHFT_X);
                y1=(xe>>(DRAW_P_SHFT_X));
                
                dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                for(;xinc<=y1;)	
                {
                    FILE_BMP;
                }	
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }		
            
        }
        return;
}
#else

#endif
//---------------------------------------------------------------------
else if(y2<y3)//the butt part
{		
    
    xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
    ks=((x1-x3))*xs;				//slope 1 --> 2
    ke=((x2-x3))*xs;				//slope 1 --> 3
    if(ks>ke)						//start slope must be smaller than the end one
    {
        xe=((1<<SHIFR_L))/(x2-x1);	
        krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color2&OUR_RGB_MASK8800)-(color1&OUR_RGB_MASK8800))>>16)*xe;
        
        kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color2&OUR_RGB_MASK0080)-(color1&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color2&OUR_RGB_MASK0088)-(color1&OUR_RGB_MASK0088)))*xe;
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xs=x1<<SHIFR_L;
        xe=x2<<SHIFR_L;
    }	
    else if(ks<ke)	
    {
        xe=((1<<SHIFR_L))/(x1-x2);	
        krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xe;
        
        kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xe;
        
        OUR_SWAP(ks,ke,xs);			
        
        rs=(color2&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        gs=(color2&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color2&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xe=x1<<SHIFR_L;
        xs=x2<<SHIFR_L;
    }
    else
    {
        return;
    }								
    for(yinc=y1;yinc<y3;yinc++)		//scan lines
    {	
        
        k_r_inc=rs;
        k_g_inc=gs;
        k_b_inc=bs;
        //------------------------------------------------------
        
        xinc=xs>>SHIFR_L;
        y1=(xe>>SHIFR_L);
        dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
        for(;xinc<=y1;)	
        {
            FILE_BMP;
        }
        rs+=krs;			
        gs+=kgs;			
        bs+=kbs;
        
        dest+=mempitch;			//point to next line
        xs+=ks;					//next line xs
        xe+=ke;	
    }		
    return;
}

}

//------------------------------------------------------
void OUR_Draw_BMP_CLIP_2D16(	int x1,int y1,
                            int x2,int y2,
                            int x3,int y3,
                            CLR color1,
                            CLR color2,
                            CLR color3,
                            UCHAR *dest_buffer, int mempitch,
                            OUR_BMP_INFO* bmp
                            )
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR rs,re,gs,ge,bs,be;
    int krs,kre,kgs,kge,kbs,kbe;
    int  k_r_inc;
    int  k_g_inc;
    int  k_b_inc;
    CLR16 *dest;
    /*
    x1=10<<1;
    y1=10<<1;
    x2=10<<1;
    y2=265<<1;
    x3=265<<1;
    y3=265<<1;
    
      
        
          color3 = 0x000080;
          color1 = 0x008080;
          color2 = 0x000000;
    */
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
        OUR_SWAP(color1,color2,rs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
        OUR_SWAP(color1,color3,rs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
        OUR_SWAP(color2,color3,rs);
    }								//18t
    
    //mempitch>>=2;					//(sizeof(dest)/2);
    dest = (CLR16*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
    if(y1<y2 )						//the top part
    {
        xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
        xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
        ks=((x1-x2))*xs;				//slope 1 --> 2
        ke=((x1-x3))*xe;				//slope 1 --> 3
        xs>>=(DRAW_P_SHFT_X-SHIFR_L);
        xe>>=(DRAW_P_SHFT_X-SHIFR_L);
        if(ks>ke)						//start slope must be smaller than the end one
        {
            kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            
            kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
            OUR_SWAP(ks,ke,xs);
        }	
        else if(ks<ke)	
        {
            krs=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            kre=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            
            kgs=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kge=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbs=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbe=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
        }
        else
        {
            return;
        }
#define BMP_KKKK 5
        //this place must be carefull
        if((ke-ks)<(1<<DRAW_P_SHFT_X))
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L-2)))/((ke-ks));// 28  -- 10 -20 =18 -10
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X) )
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            kge= (kge-kgs);
            if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
                kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);	
            else
                kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);	
            
        }
        else
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L)))/((ke-ks)>>3);
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X))
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            kge= (kge-kgs);
            if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
                kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);	
            else
                kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);	
        }
        xs=(x1<<(DRAW_P_SHFT_X));
        xe=(x1<<(DRAW_P_SHFT_X));
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);		
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);		
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        //clip ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
        yinc=y1;
        if(y1<0)
        {
            if(y2>0)
            {
                rs-=y1*krs;			
                gs-=y1*kgs;			
                bs-=y1*kbs;
                
                dest-=y1*mempitch;			//point to next line
                xs-=y1*ks;					//next line xs
                xe-=y1*ke;					//next line xe
                yinc=0;
            }
            else
            {
                y1=y1-y2;
                rs-=y1*krs;			
                gs-=y1*kgs;			
                bs-=y1*kbs;
                
                dest-=y1*mempitch;			//point to next line
                xs-=y1*ks;					//next line xs
                xe-=y1*ke;					//next line xe
                yinc=0;
                goto P3;
            }/**/
        }
        if(y2>=OUR_D_screem_h)
        {
            for(;yinc<OUR_D_screem_h;yinc++)		//scan lines
            {	
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------	
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                }
                for(;xinc<=y1;)	
                {
                    FILE_BMP;
                }		
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }
            return;
        }
        //ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
        //else if(y2>0)			
        {
            for(;yinc<y2;yinc++)		//scan lines
            {	
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------	
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                }
                for(;xinc<=y1;)	
                {
                    FILE_BMP;
                }		
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }
        }
P3:	
        if(y2<y3)						//the butt part
        {	
            y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
            x1=((x2-x3))*y1;
            y1>>=(DRAW_P_SHFT_X-SHIFR_L);
            if(x1>ke)
            {
                ks=x1;
                
                krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*y1;			
                kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*y1;				
                kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*y1;
            }
            else
            {
                ke=x1;
            }
            //ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
            if(y2<0)
            {
                rs-=y2*krs;			
                gs-=y2*kgs;			
                bs-=y2*kbs;
                
                dest-=y2*mempitch;			//point to next line
                xs-=y2*ks;					//next line xs
                xe-=y2*ke;					//next line xe
                
            }/**/
            if(y3>OUR_D_screem_h-1)
            {
                y3=OUR_D_screem_h-1;
            }
            //ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
            for(;yinc<=y3;yinc++)
            {			
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------
                xinc=xs>>(DRAW_P_SHFT_X);
                if(xinc<0)
                {
                    k_r_inc-=xinc*kre;
                    k_g_inc-=xinc*kge;
                    k_b_inc-=xinc*kbe;
                    xinc=0;
                }
                y1=(xe>>(DRAW_P_SHFT_X));
                if(y1>=OUR_D_screem_w)
                {
                    y1=OUR_D_screem_w-1;
                }
                else if(y1>0)
                {
                    dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                }
                for(;xinc<=y1;)	
                {
                    FILE_BMP;
                }	
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }		
            
        }
        return;
}
#else

#endif
//---------------------------------------------------------------------
else if(y2<y3)//the butt part
{		
    
    xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
    ks=((x1-x3))*xs;				//slope 1 --> 2
    ke=((x2-x3))*xs;				//slope 1 --> 3
    if(ks>ke)						//start slope must be smaller than the end one
    {
        xe=((1<<SHIFR_L))/(x2-x1);	
        krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color2&OUR_RGB_MASK8800)-(color1&OUR_RGB_MASK8800))>>16)*xe;
        
        kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color2&OUR_RGB_MASK0080)-(color1&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color2&OUR_RGB_MASK0088)-(color1&OUR_RGB_MASK0088)))*xe;
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xs=x1<<SHIFR_L;
        xe=x2<<SHIFR_L;
    }	
    else if(ks<ke)	
    {
        xe=((1<<SHIFR_L))/(x1-x2);	
        krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xe;
        
        kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xe;
        
        OUR_SWAP(ks,ke,xs);			
        
        rs=(color2&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        gs=(color2&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color2&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xe=x1<<SHIFR_L;
        xs=x2<<SHIFR_L;
    }
    else
    {
        return;
    }
    //clip
    if(y1<0)
    {
        rs-=y1*krs;			
        gs-=y1*kgs;			
        bs-=y1*kbs;
        
        dest-=y1*mempitch;			//point to next line
        xs-=y1*ks;					//next line xs
        xe-=y1*ke;					//next line xe
        y1=0;
    }
    if(y3>=OUR_D_screem_h)	
    {
        y3=OUR_D_screem_h;
    }
    for(yinc=y1;yinc<y3;yinc++)		//scan lines
    {	
        
        k_r_inc=rs;
        k_g_inc=gs;
        k_b_inc=bs;
        //------------------------------------------------------
        xinc=xs>>SHIFR_L;
        y1=(xe>>SHIFR_L);
        if(xinc<0)
        {
            k_r_inc-=xinc*kre;
            k_g_inc-=xinc*kge;
            k_b_inc-=xinc*kbe;
            xinc=0;
        }
        if(y1>=OUR_D_screem_w)
        {
            y1=OUR_D_screem_w-1;
        }
        else if(y1>0)
        {
            dest[xinc++] =((CLR16 *)bmp->bmp_data)[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
        }
        for(;xinc<=y1;)	
        {
            FILE_BMP;
        }
        rs+=krs;			
        gs+=kgs;			
        bs+=kbs;
        
        dest+=mempitch;			//point to next line
        xs+=ks;					//next line xs
        xe+=ke;	
    }		
    return;
}

}
#endif
//=======================================================================

void OUR_Draw_flat16( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch)
{
    
    UINT i1,i2,i3;
    UINT poly;
    UINT num;
    UINT poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;
    for (poly=0; poly <num; poly++)
    {
        //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
        {
            poly_change=obj->pTriangle_changed[poly];
            i1=triangle_work[poly_change].p1;
            i2=triangle_work[poly_change].p2;
            i3=triangle_work[poly_change].p3;
            
#if 1
            OUR_Draw_Triangle_CLIP_2D16(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                // triangle_work[poly_change].CC_work.clor,
                obj->clor_work[poly_change],
                video_buffer, lpitch);
#else
            OUR_Draw_Triangle_2D16(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                // triangle_work[poly_change].CC_work.clor,
                obj->clor_work[poly_change],
                video_buffer, lpitch);
#endif
            OUR_D_triangle_drawed_num++;
            
        }
    }
} 
void OUR_Draw_Gouraud16( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch)
{
    UINT i1,i2,i3;
    UINT poly;
    UINT num;
    UINT poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;
    for (poly=0; poly <num; poly++)
    {
        //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
        {
            poly_change=obj->pTriangle_changed[poly];
            i1=triangle_work[poly_change].p1;
            i2=triangle_work[poly_change].p2;
            i3=triangle_work[poly_change].p3;
#if 1
            OUR_Draw_Gouraud_CLIP_2D16(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                obj->clor_work[i1],
                obj->clor_work[i2],
                obj->clor_work[i3],
                video_buffer, lpitch);
#else
            OUR_Draw_Gouraud_2D16(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                obj->clor_work[i1],
                obj->clor_work[i2],
                obj->clor_work[i3],
                video_buffer, lpitch);
#endif
            OUR_D_triangle_drawed_num++;
            
        }
    }
} 

void OUR_Draw_bmp16( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch)
{
    UINT i1,i2,i3;
    UINT poly;
    UINT num;
    UINT poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;
    for (poly=0; poly <num; poly++)
    {
        //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
        {
            poly_change=obj->pTriangle_changed[poly];
            i1=triangle_work[poly_change].p1;
            i2=triangle_work[poly_change].p2;
            i3=triangle_work[poly_change].p3;
#if 1
            OUR_Draw_BMP_CLIP_PRE_2D16(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i1].MM.z,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i2].MM.z,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                point_work[i3].MM.z,
                triangle_work[poly_change].uv1,
                triangle_work[poly_change].uv2,
                triangle_work[poly_change].uv3,
                video_buffer, lpitch,
                (obj->bmp_info[triangle_work[poly_change].bmp_index]));
#else 
    OUR_Draw_BMP_CLIP_PRE_2D16_Q(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i1].MM.z,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i2].MM.z,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                point_work[i3].MM.z,
                triangle_work[poly_change].uv1,
                triangle_work[poly_change].uv2,
                triangle_work[poly_change].uv3,
                video_buffer, lpitch,
                (obj->bmp_info[triangle_work[poly_change].bmp_index])
                //&BMP_IFO_bmp0
                );
#endif
            OUR_D_triangle_drawed_num++;
            
        }
    }
} 

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
#define SBLT_SHIFT 18

void OUR_Draw_bmp16_BITL( int x1,int y1,int x2,int y2,
                                                        int x3,int y3,
                                                        UCHAR *video_buffer, int lpitch,
                                                        OUR_BMP_INFO* bmp)
{
    int line,row;
    //int x_inc,y_inc;
    int xs,ys;
    CLR16 *dest;
    CLR16 *src;
    
    //x_inc=((x4)<<SBLT_SHIFT)/(x2);
    //y_inc=((y4)<<SBLT_SHIFT)/(y2);
    ys=y3;//<<SBLT_SHIFT;
    //x3=x3<<SBLT_SHIFT;
    y2=y1+y2-1;
    if(y1<0)
    {
        //ys-=y1*y_inc;
        y1=0;
    }
    if(y1>=LCD_H)
    {
        return;
    }
    if(y2<0)
    {
        return;
    }
    if(y2>=LCD_H)
    {
        ys+=(y2-(LCD_H-1));
        y2=LCD_H-1;
    }
    
    x2=x1+x2-1;
    if(x1<0)
    {
        x3-=x1;
        x1=0;
    }
    if(x1>=LCD_W)
    {
        return;
    }
    if(x2<0)
    {
        return;
    }
    if(x2>=LCD_W)
    {
        x2=LCD_W-1;
    }
    //===================================================
    //src=(CLR16 *)bmp->bmp_data;
    dest = (CLR16*)video_buffer +(y1)*lpitch;
    src=(CLR16 *)bmp->bmp_data+((bmp->height-1-ys)*bmp->width);
    for(line=y1;line<=y2;line++)
    {
        xs=x3;
#if 0       
        for (row=x1;row<=x2;row++)
        {
            dest[row]=src[xs];
            xs+=1;
        }
#else
#define CPY_BMP {dest[row]=src[xs]; xs+=1;  row+=1;}
        x2-=16;
        for (row=x1;row<=x2;)
        {
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            CPY_BMP;
            
        }
        x2+=16;
        for (;row<=x2;row++)
        {
            dest[row]=src[xs];
            xs+=1;
        }
#endif
        src-=bmp->width;
        dest+=lpitch;
    }
}


void OUR_Draw_bmp16_SBLT( int x1,int y1,int x2,int y2,
                                                        int x3,int y3,int x4,int y4,
                                                        UCHAR *video_buffer, int lpitch,
                                                        OUR_BMP_INFO* bmp)
{
    int line,row;
    int x_inc,y_inc;
    int xs,ys;
    CLR16 *dest;
    CLR16 *src;
    
    x_inc=((x4)<<SBLT_SHIFT)/(x2);
    y_inc=((y4)<<SBLT_SHIFT)/(y2);
    ys=y3<<SBLT_SHIFT;
    x3=x3<<SBLT_SHIFT;
    y2=y1+y2-1;
    if(y1<0)
    {
        //ys-=y1*y_inc;
        y1=0;
    }
    if(y1>=LCD_H)
    {
        return;
    }
    if(y2<0)
    {
        return;
    }
    if(y2>=LCD_H)
    {
        ys+=(y2-(LCD_H-1))*y_inc;
        y2=LCD_H-1;
    }
    
    x2=x1+x2-1;
    if(x1<0)
    {
        x3-=x1*x_inc;
        x1=0;
    }
    if(x1>=LCD_W)
    {
        return;
    }
    if(x2<0)
    {
        return;
    }
    if(x2>=LCD_W)
    {
        x2=LCD_W-1;
    }
    //===================================================
    src=(CLR16 *)bmp->bmp_data;
    dest = (CLR16*)video_buffer +(y2)*lpitch;
    for(line=y1;line<=y2;line++)
    {
        xs=x3;
        src=(CLR16 *)bmp->bmp_data+((ys>>SBLT_SHIFT)*bmp->width);
        for (row=x1;row<=x2;row++)
        {
            dest[row]=src[xs>>SBLT_SHIFT];
            xs+=x_inc;
        }
        ys+=y_inc;
        dest-=lpitch;
    }
}


void OUR_Draw_bmp16_SBLT_T( int x1,int y1,int x2,int y2,
                                                        int x3,int y3,int x4,int y4,
                                                        int t,
                                                        UCHAR *video_buffer, int lpitch,
                                                        OUR_BMP_INFO* bmp)
{
    int line,row;
    int x_inc,y_inc;
    int xs,ys;
    CLR16 *dest;
    CLR16 *src;
    UINT c,d;

    
    x_inc=((x4)<<SBLT_SHIFT)/(x2);
    y_inc=((y4)<<SBLT_SHIFT)/(y2);
    ys=y3<<SBLT_SHIFT;
    y2=y1+y2-1;
    if(y1<0)
    {
        y1=0;
    }
    if(y1>=LCD_H)
    {
        return;
    }
    if(y2<0)
    {
        return;
    }
    if(y2>=LCD_H)
    {
        y2=LCD_H-1;
    }
    
    x2=x1+x2-1;
    if(x1<0)
    {
        x1=0;
    }
    if(x1>=LCD_W)
    {
        return;
    }
    if(x2<0)
    {
        return;
    }
    if(x2>=LCD_W)
    {
        x2=LCD_W-1;
    }
    //========================================================
    src=(CLR16 *)bmp->bmp_data;
    dest = (CLR16*)video_buffer +(y2)*lpitch;
    for(line=y1;line<=y2;line++)
    {
        xs=x3<<SBLT_SHIFT;
        src=(CLR16 *)bmp->bmp_data+((ys>>SBLT_SHIFT)*bmp->width);
        for (row=x1;row<=x2;row++)
        {
            c=dest[row]+(dest[row]<<16);
            d=src[xs>>SBLT_SHIFT]+(src[xs>>SBLT_SHIFT]<<16);
            c=((((c&0x07e0f81f)*(32-t)+(d&0x07e0f81f)*t)>>5 ) &(0x07e0f81f)) ;
            dest[row]=c+(c>>16) ;
            //15            
            xs+=x_inc;
        }
        ys+=y_inc;
        dest-=lpitch;
    }
}


void OUR_Draw_bmp16_SBLT_KEY( int x1,int y1,int x2,int y2,
                                                        int x3,int y3,int x4,int y4,
                                                        CLR16 key,
                                                        UCHAR *video_buffer, int lpitch,
                                                        OUR_BMP_INFO* bmp)
{
    int line,row;
    int x_inc,y_inc;
    int xs,ys;
    CLR16 *dest;
    CLR16 *src;
    UINT c,d;

    
    x_inc=((x4)<<SBLT_SHIFT)/(x2);
    y_inc=((y4)<<SBLT_SHIFT)/(y2);
    ys=y3<<SBLT_SHIFT;
    x3=x3<<SBLT_SHIFT;
    y2=y1+y2-1;
    if(y1<0)
    {
        //ys-=y1*y_inc;
        y1=0;
    }
    if(y1>=LCD_H)
    {
        return;
    }
    if(y2<0)
    {
        return;
    }
    if(y2>=LCD_H)
    {
        ys+=(y2-(LCD_H-1))*y_inc;
        y2=LCD_H-1;
    }
    
    x2=x1+x2-1;
    if(x1<0)
    {
        x3-=x1*x_inc;
        x1=0;
    }
    if(x1>=LCD_W)
    {
        return;
    }
    if(x2<0)
    {
        return;
    }
    if(x2>=LCD_W)
    {
        x2=LCD_W-1;
    }
    //====================================================
    src=(CLR16 *)bmp->bmp_data;
    dest = (CLR16*)video_buffer +(y2)*lpitch;
    for(line=y1;line<=y2;line++)
    {
        xs=x3;
        src=(CLR16 *)bmp->bmp_data+((ys>>SBLT_SHIFT)*bmp->width);
        for (row=x1;row<=x2;row++)
        {
            c=src[xs>>SBLT_SHIFT];
            if(c!=key)
            {
                dest[row]=c;
            }
            //15            
            xs+=x_inc;
        }
        ys+=y_inc;
        dest-=lpitch;
    }
}




void OUR_Draw_CLR16_SBLT_Tbmp( int x1,int y1,int x2,int y2,
                                                        int x3,int y3,int x4,int y4,
                                                        CLR16 clr,
                                                        UCHAR *video_buffer, int lpitch,
                                                        OUR_BMP_INFO* bmp)
{
    int line,row;
    int x_inc,y_inc;
    int xs,ys;
    CLR16 *dest;
    char *tp;
    char t;
    UINT c,d;

    x_inc=((x4)<<SBLT_SHIFT)/(x2);
    y_inc=((y4)<<SBLT_SHIFT)/(y2);
    ys=y3<<SBLT_SHIFT;
    x3=x3<<SBLT_SHIFT;
    y2=y1+y2-1;
    if(y1<0)
    {
        //ys-=y1*y_inc;
        y1=0;
    }
    if(y1>=LCD_H)
    {
        return;
    }
    if(y2<0)
    {
        return;
    }
    if(y2>=LCD_H)
    {
        ys+=(y2-(LCD_H-1))*y_inc;
        y2=LCD_H-1;
    }
    
    x2=x1+x2-1;
    if(x1<0)
    {
        x3-=x1*x_inc;
        x1=0;
    }
    if(x1>=LCD_W)
    {
        return;
    }
    if(x2<0)
    {
        return;
    }
    if(x2>=LCD_W)
    {
        x2=LCD_W-1;
    }
    //======================================================
    c=clr+(clr<<16);
    tp=(char *)bmp->t_data;
    dest = (CLR16*)video_buffer +(y2)*lpitch;
    for(line=y1;line<=y2;line++)
    {
        xs=x3;
        tp=(char *)bmp->t_data+((ys>>SBLT_SHIFT)*bmp->width);
        for (row=x1;row<=x2;row++)
        {
            t=tp[xs>>SBLT_SHIFT];
            d=dest[row]+(dest[row]<<16);
            d=((((d&0x07e0f81f)*(32-t)+(c&0x07e0f81f)*t)>>5 ) &(0x07e0f81f)) ;
            
            dest[row]=d+(d>>16) ;
            //15            
            xs+=x_inc;
        }
        ys+=y_inc;
        dest-=lpitch;
    }
}


//=========================================================

void OUR_Draw_CLR16_SBLT_KEY_Tbmp( int x1,int y1,int x2,int y2,
                                                        int x3,int y3,int x4,int y4,
                                                        int x5,int y5,int x6,int y6,
                                                        CLR16 clr,
                                                        CLR16 clr_b,
                                                        char clr_key,
                                                        UCHAR *video_buffer, int lpitch,
                                                        OUR_BMP_INFO* bmp_t,
                                                        OUR_BMP_INFO* bmp_k)
{
    int line,row;
    int x_inc,y_inc;
    int xs,ys;
    int x_inc5,y_inc5;
    int xs5,ys5;
    CLR16 *dest;
    char *tp;
    char *kp;
    char t;
    char k;
    UINT c,d,b;

    x_inc5=((x6)<<SBLT_SHIFT)/(x2);
    y_inc5=((y6)<<SBLT_SHIFT)/(y2);
    ys5=y5<<SBLT_SHIFT;
    x5=x5<<SBLT_SHIFT;

    x_inc=((x4)<<SBLT_SHIFT)/(x2);
    y_inc=((y4)<<SBLT_SHIFT)/(y2);
    ys=y3<<SBLT_SHIFT;
    x3=x3<<SBLT_SHIFT;
    y2=y1+y2-1;
    if(y1<0)
    {
        //ys-=y1*y_inc;
        y1=0;
    }
    if(y1>=LCD_H)
    {
        return;
    }
    if(y2<0)
    {
        return;
    }
    if(y2>=LCD_H)
    {
        ys+=(y2-(LCD_H-1))*y_inc;
        ys5+=(y2-(LCD_H-1))*y_inc5;
        y2=LCD_H-1;
    }
    
    x2=x1+x2-1;
    if(x1<0)
    {
        x3-=x1*x_inc;
        x5-=x1*x_inc5;
        x1=0;
    }
    if(x1>=LCD_W)
    {
        return;
    }
    if(x2<0)
    {
        return;
    }
    if(x2>=LCD_W)
    {
        x2=LCD_W-1;
    }
    //======================================================
    c=clr+(clr<<16);
    d=clr_b+(clr_b<<16);
    tp=(char *)bmp_t->t_data;
    kp=(char *)bmp_k->t_data;
    dest = (CLR16*)video_buffer +(y2)*lpitch;
    for(line=y1;line<=y2;line++)
    {
        xs=x3;
        xs5=x5;
        tp=(char *)bmp_t->t_data+((ys>>SBLT_SHIFT)*bmp_t->width);
        kp=(char *)bmp_k->t_data+((ys5>>SBLT_SHIFT)*bmp_k->width);
        for (row=x1;row<=x2;row++)
        {
            if(clr_key<kp[xs5>>SBLT_SHIFT])
            {
                t=tp[xs>>SBLT_SHIFT];
                //d=dest[row]+(dest[row]<<16);
                b=((((d&0x07e0f81f)*(32-t)+(c&0x07e0f81f)*t)>>5 ) &(0x07e0f81f)) ;
                dest[row]=b+(b>>16) ;
            }
            //15            
            xs+=x_inc;         
            xs5+=x_inc5;
        }
        ys+=y_inc;
        ys5+=y_inc5;
        dest-=lpitch;
    }
}


void OUR_Draw_rote_bmp16(CLR16* src, int h,int w, int f)
{
    //int a[]={0,10};//240*320
    //int a[]={0,2,6,20};//240*400
    //int a[]={0,1,2,6,13,16};//640*480
    //int a[]={0,1,4,9};//1024*768
    //int a[]={0,1,6,10,12,16,22,33};//1600*1200
   /* int a[]={0,     1,      5,      6,      8,      10,     12,     13,     16,     17,//320*256
                20,   25,    41,     46,     60,    76,     82,     88,     90,     93,//320*256
                140, 165,   248,   281,   328,   516,   580,    746,    798,   912,//320*256
                986, 1161,   1410,   1742,   3618,   3900,   7801,    11702//320*256
                
                };*/
/*
    const int a[]={//1280*1024
    0x00000000,0x00000001,0x00000002,0x00000003,0x00000005,0x00000006,0x00000007,0x00000008,0x0000000a,0x0000000b,
    0x0000000c,0x0000000d,0x0000000f,0x00000010,0x00000011,0x00000012,0x00000014,0x00000015,0x00000016,0x00000017,
    0x00000019,0x0000001a,0x0000001b,0x0000001e,0x00000021,0x00000023,0x00000024,0x00000025,0x00000026,0x00000029,
    0x0000002a,0x0000002b,0x0000002d,0x0000002f,0x00000030,0x00000032,0x00000034,0x00000035,0x00000038,0x0000003a,
    0x0000003e,0x00000042,0x00000043,0x00000046,0x00000047,0x00000048,0x00000049,0x0000004c,0x0000004d,0x0000004e,
    0x00000050,0x00000052,0x00000053,0x00000055,0x0000005a,0x0000005c,0x00000060,0x00000061,0x00000064,0x00000066,
    0x00000069,0x0000006a,0x0000006b,0x0000006e,0x00000070,0x00000071,0x00000074,0x00000076,0x0000007d,0x00000082,
    0x00000085,0x00000087,0x00000088,0x0000008d,0x00000091,0x00000092,0x00000096,0x00000098,0x00000099,0x0000009c,
    0x000000a1,0x000000a5,0x000000aa,0x000000ac,0x000000b5,0x000000c0,0x000000c1,0x000000c4,0x000000c6,0x000000c8,
    0x000000d2,0x000000d3,0x000000d5,0x000000d7,0x000000d8,0x000000dd,0x000000e9,0x000000ec,0x000000ee,0x00000100,
    0x00000102,0x0000010b,0x0000010e,0x00000111,0x00000120,0x0000012c,0x0000012d,0x00000133,0x0000014b,0x0000014c,
    0x00000155,0x0000015e,0x00000166,0x00000183,0x00000184,0x0000018c,0x00000191,0x0000019a,0x000001b0,0x000001b8,
    0x000001c0,0x000001d4,0x00000217,0x0000021d,0x00000223,0x0000025a,0x00000274,0x00000299,0x000002e0,0x00000323,
    0x00000366,0x000003a9,0x0000042f,0x00000472,0x000004b5,0x000004f8,0x0000057e,0x000005c1,0x00000604,0x00000647,
    0x000006cd,0x00000710,0x00000753,0x0000081c,0x000008e5,0x0000096b,0x000009ae,0x000009f1,0x00000a34,0x00000afd,
    0x00000b40,0x00000b83,0x00000c09,0x00000c8f,0x00000cd2,0x00000d58,0x00000dde,0x00000eea,0x00000f70,0x0000107c,
    0x000011cb,0x000012d7,0x0000135d,0x00001469,0x000014ac,0x00001532,0x000015b8,0x000015fb,0x00001681,0x000017d0,
    0x00001856,0x00001962,0x00001978,0x00001af4,0x00001bbd,0x00001d92,0x00002397,0x00002d46,0x0000377b,0x000038ca,
    0x00004342,0x00004c6a,0x0000b24e};*/
    const int a[]={ // 2048*1536
    0x00000000,0x00000001,0x00000002,0x00000003,0x00000004,0x00000005,0x00000006,0x00000007,0x00000008,0x00000009,
    0x0000000a,0x0000000c,0x0000000d,0x0000000e,0x00000010,0x00000011,0x00000012,0x00000013,0x00000014,0x00000015,
    0x00000016,0x00000018,0x00000019,0x0000001a,0x0000001b,0x0000001c,0x0000001d,0x0000001e,0x00000020,0x00000021,
    0x00000022,0x00000026,0x00000027,0x00000028,0x00000029,0x0000002a,0x0000002c,0x0000002d,0x0000002e,0x00000030,
    0x00000031,0x00000032,0x00000033,0x00000034,0x00000035,0x00000037,0x00000038,0x00000039,0x0000003a,0x0000003c,
    0x0000003e,0x00000040,0x00000041,0x00000043,0x00000044,0x00000045,0x0000004a,0x0000004c,0x00000050,0x00000051,
    0x00000052,0x00000054,0x00000055,0x00000056,0x00000059,0x0000005b,0x0000005c,0x0000005d,0x0000005e,0x00000060,
    0x00000061,0x00000062,0x00000065,0x00000066,0x00000069,0x0000006a,0x00000073,0x00000076,0x00000079,0x0000007d,
    0x00000082,0x00000089,0x0000008a,0x0000008b,0x00000092,0x00000099,0x0000009a,0x0000009c,0x0000009e,0x000000a1,
    0x000000a3,0x000000a9,0x000000aa,0x000000ab,0x000000b2,0x000000ba,0x000000bc,0x000000c1,0x000000c3,0x000000c5,
    0x000000c6,0x000000cc,0x000000de,0x000000e6,0x000000e7,0x000000ea,0x000000f3,0x000000f8,0x00000104,0x0000010e,
    0x0000011c,0x00000120,0x00000122,0x00000125,0x00000126,0x00000139,0x0000013d,0x00000153,0x00000156,0x00000164,
    0x00000188,0x000001a0,0x000001b6,0x000001c0,0x000001cd,0x000001d5,0x000001d6,0x000001dc,0x0000021a,0x00000236,
    0x00000245,0x0000029c,0x000002d6,0x00000884,0x000010e6,0x000021cd,0x000032b4,0x0000439b,0x00005482,0x00006569,
    0x00007650,0x00008737,0x0000981e,0x0000a905,0x0000b9ec,0x0000dbba,0x0000eca1,0x0000fd88,0x00011f56,0x0001303d,
    0x00014124,0x0001520b,0x000162f2,0x000173d9,0x000184c0,0x0001a68e,0x0001b775,0x0001c85c,0x0001d943,0x0001ea2a,
    0x0001fb11,0x00022dc6,0x00023ead,0x00024f94,0x00029330,0x0002a417,0x0002b4fe,0x0002d6cc,0x0002f89a,0x00031a68,
    0x00034d1d,0x00035e04,0x00036eeb,0x000390b9,0x0003b287,0x0003c36e,0x0003e53c,0x0004070a,0x000428d8,0x00044aa6,
    0x00045b8d,0x00047d5b,0x00048e42,0x00049f29,0x0004f3ac,0x00055916,0x00057ae4,0x00059cb2,0x0005ad99,0x000634d1,
    0x00068954,0x0006bc09,0x0006ccf0,0x0007108c,0x00093e53,0x000ab22d,0x000b3965,0x000b4a4c
};
    
    int i,j;
    int d,s;
    CLR16 tmp1,tmp2;
    for(i=0;i<sizeof(a)/4;i++)
    {
        d=a[i];
        tmp2=src[d];
        while(1)
        {
            s=d;
            //d=((d%w)*h)+h-(d/w);
            d=(w-1-(d%w))*h+(d/w);
            tmp1=src[d];
            src[d]=tmp2;
            tmp2=tmp1;
            
            if(d==a[i])
            {
                break;
            }
        }
    }
}
//==================================================================

void OUR_Draw_falsh_board16(int x1,int y1,int x2,int y2,int width,
                                                        CLR C_start,CLR C_end,
                                                        UCHAR *video_buffer, int lpitch)
{
    int line,row;
    int x_inc,y_inc;
    int rs,gs,bs;
    int r_inc,g_inc,b_inc;
    CLR16 *dest,*dest_top;
    CLR src32;
    CLR16 src;
    x2=x1+x2-1;
    y2=y1+y2-1;
    
    r_inc= -((C_start&OUR_RGB_MASK0800) -(C_end&OUR_RGB_MASK0800))/width;
    g_inc= -(((C_start&OUR_RGB_MASK0080) -(C_end&OUR_RGB_MASK0080))<<8)/width;
    b_inc= -(((C_start&OUR_RGB_MASK0008) -(C_end&OUR_RGB_MASK0008))<<16)/width;
    dest = (CLR16*)video_buffer +(y1)*lpitch;
    rs=(C_start&OUR_RGB_MASK0800);
    gs=(C_start&OUR_RGB_MASK0080)<<8;
    bs=(C_start&OUR_RGB_MASK0008)<<16;
    //top
    for (y_inc=0;y_inc<width;y_inc++)
    { 
        src32=(rs&OUR_RGB_MASK0800)+((gs&OUR_RGB_MASK0800)>>8)+((bs&OUR_RGB_MASK0800)>>16);
        src=OUR_RGB32_2_16(src32);
        for (x_inc=x1+width;x_inc<x2-width;x_inc++)
        {
            dest[x_inc]=src;
        }
        rs+=r_inc;
        gs+=g_inc;
        bs+=b_inc;
        dest+=lpitch;
    }
    //top right
    dest = (CLR16*)video_buffer +(y1)*lpitch;
    for (y_inc=0;y_inc<width;y_inc++)
    { 
        rs=(C_start&OUR_RGB_MASK0800);
        gs=(C_start&OUR_RGB_MASK0080)<<8;
        bs=(C_start&OUR_RGB_MASK0008)<<16;
        //src32=(rs&OUR_RGB_MASK0800)+((gs&OUR_RGB_MASK0800)>>8)+((bs&OUR_RGB_MASK0800)>>16);
        //src=OUR_RGB32_2_16(src32);
        for (x_inc=x2-width+y_inc;x_inc>=x2-width;x_inc--)
        { 
            src32=(rs&OUR_RGB_MASK0800)+((gs&OUR_RGB_MASK0800)>>8)+((bs&OUR_RGB_MASK0800)>>16);
            src=OUR_RGB32_2_16(src32);
            dest[x_inc]=src;
            rs+=r_inc;
            gs+=g_inc;
            bs+=b_inc;
        }
        dest+=lpitch;
    }
    //bott
    dest_top= (CLR16*)video_buffer +(y1)*lpitch;
    dest = (CLR16*)video_buffer +(y2)*lpitch;
    for (y_inc=0;y_inc<width;y_inc++)
    { 
        for (x_inc=x1+width-y_inc;x_inc<=x2-width;x_inc++)
        {
            dest[x_inc] = dest_top[x2+x1-x_inc];//
        }
        dest-=lpitch;
        dest_top+=lpitch;
    }  
    //left
    dest_top= (CLR16*)video_buffer +(y1)*lpitch;
    for (y_inc=x1;y_inc<x1+width;y_inc++)
    { 
        dest = (CLR16*)video_buffer +(y1+width-y_inc+x1)*lpitch;
        for (x_inc=y1+width-y_inc+x1;x_inc<=y2-width;x_inc++)
        {
            dest[y_inc] = dest_top[x2+y1-x_inc];//
            dest+=lpitch;
        }
        dest_top+=lpitch;
    }  
    //right
    dest_top= (CLR16*)video_buffer +(y1)*lpitch;
    for (y_inc=x2;y_inc>x2-width;y_inc--)
    { 
        dest = (CLR16*)video_buffer +(y2-y_inc+x2-width)*lpitch;//-width+y_inc-x2 +y_inc-x2
        for (x_inc=y2-y_inc+x2-width;x_inc>=y1+width;x_inc--)
        {
            dest[y_inc] = dest_top[x2-y2+x_inc];//+width-y_inc+x2
            dest-=lpitch;
        }
        dest_top+=lpitch;
    }
    
}

//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
#endif

